<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>企业管理</title>
    <link th:replace="/comm/header :: header"/>

    <script type="text/javascript">

        function formatter(value, row, index) {
            if (row.ischeck == true) {
                return '已认证'
            } else {
                return '未认证'
            }
        }

        function rowStyler(index, row) {
            if (row.sex == 1) {
                return 'background-color:#6293BB;color:#fff;'; // return inline style
            } else {
                return 'color:green;';
            }
        }

        function formatEdit(val, row, index) {
            var str = '';
            if ($("#enterpriseupdate").length > 0) {
                str += $.formatString('<a href="javascript:void(0)" class="enterprise-easyui-linkbutton-edit primary" onclick="editEnterpriseFun(\'{0}\');" >编辑</a>', index);
            }
            if ($("#enterprisedelete").length > 0) {
                str += $.formatString('<a href="javascript:void(0)" class="enterprise-easyui-linkbutton-del error"  onclick="deleteEnterpriseFun(\'{0}\');" >删除</a>', row.eid);
            }
            if ($("#enterprischeck").length > 0&&!row.ischeck) {
                str += $.formatString('<a href="javascript:void(0)" class="enterprise-easyui-linkbutton-ischeck error"  onclick="isCheck(\'{0}\');" >认证</a>', row.eid);
            }else {
                str += $.formatString('<a href="javascript:void(0)" class="enterprise-easyui-linkbutton-nocheck error"  aria-disabled="true" >已认证</a>');
            }
            return str;
        }

        function searchEnterprise() {
            $("#dg").datagrid('load', {
                "cName": $("#s_EnterpriseName").val()
            });
        }


        function closeEnterpriseDialog() {
            $("#dlg").dialog("close");
        }

        function saveEnterprise() {
            var file = $("#file")[0].files[0];
            $("#fm").form("submit", {
                url: "/sys/ent/save",
                mehtod: "post",
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.flag) {
                        message("保存成功");
                        $("#dg").datagrid("reload");
                        $("#dlg").dialog("close");
                    } else {
                        message(result.message);
                    }
                }
            });
        }

        function isCheck(eid) {
            $.post({
                url: "/sys/ent/save?",
                data:{eid:eid,ischeck:true},
                success:function (rs) {
                    if(rs.flag){
                        message("认证成功");
                    }else {
                        message(result.message);
                    }
                }
            })
        }

        function openEnterpriseAddDialog() {
            $("#dlg").dialog("open").dialog("setTitle", "添加企业信息");
            $("#fm").form('clear');
            $("#pic").attr("src", null);
        }

        function editEnterpriseFun(index) {
            $("#dg").datagrid('selectRow', index);
            var row = $("#dg").datagrid('getSelected');
            $("#pic").attr("src", "/image/show?fileName=" + row.license);
            $("#dlg").dialog("open").dialog("setTitle", "修改企业信息");
            $("#fm").form("load", row);
        }

        function deleteEnterpriseFun(eid) {
            console.log(eid);
            $.messager.confirm("系统提示", "您确定要删除这条企业及相关数据吗?", function (r) {
                if (r) {
                    $.post("/sys/ent/delete?eid=" + eid, function (result) {
                        if (result.flag) {
                            message(result.info)
                            $("#dg").datagrid("reload");
                        } else {
                            message(result.message);
                        }
                    }, "json");
                }
            });
        }
    </script>
</head>
<body style="margin: 1px">
<shiro:hasPermission name="sys:ent:delete">
    <input type="hidden" id="enterprisedelete"/>
</shiro:hasPermission>
<shiro:hasPermission name="sys:ent:update">
    <input type="hidden" id="enterpriseupdate"/>
</shiro:hasPermission>
<shiro:hasPermission name="sys:ent:update">
    <input type="hidden" id="enterprischeck"/>
</shiro:hasPermission>
<table id="dg" title="企业管理" class="easyui-datagrid"
       fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
       url="/sys/ent/list" fit="true" toolbar="#tb" method="get">
    <thead>
    <th field="eid" width="20" align="center" hidden>编号</th>
    <th field="cname" width="50" align="center">企业名称</th>
    <th field="legal" width="50" align="center">法人</th>
    <th field="photo" width="50" align="center" hidden>法人证件</th>
    <th field="license" width="50" align="center" hidden>营业执照</th>
    <th field="tel" width="50" align="center">联系电话</th>
    <th field="phone" width="50" align="center">手机</th>
    <th field="address" width="50" align="center">地址</th>
    <th field="eaddress" width="50" align="center">企业地址</th>
    <th field="ischeck" width="50" align="center" formatter="formatter" styler="rowStyler">是否认证</th>
    <th field="remark" width="50" align="center">备注信息</th>
    <th field="op" width="50" align="center" formatter="formatEdit">操作</th>
    </thead>
</table>

<div id="tb">
    <a href="javascript:openEnterpriseAddDialog()" class="easyui-linkbutton primary" iconCls="fa fa-plus" plain="true">添加</a>
    &nbsp;企业名称：&nbsp;<input type="text" id="s_EnterpriseName" size="20"
                            onkeydown="if(event.keyCode==13) searchEnterprise()"/>
    <a href="javascript:searchEnterprise()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
    <a href="javascript:" class="easyui-linkbutton" data-options="plain:true"
       onclick="$('#dg').datagrid('toExcel','enterprise.xls')">下载</a>
</div>
<div id="dlg" class="easyui-dialog" style="width: 600px;height: 700px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post">
        <input type="hidden" id="id" name="eid">
        <table cellspacing="8px">
            <tr>
                <td align="right">企业名称：</td>
                <td>
                    <input type="text" style="width:200px" id="cname" name="cname"
                           class="easyui-validatebox easyui-textbox"
                           data-options="required:true,validType:'length[3,10]'"/>
                </td>
                <td id="vename"><span style="color: red;display: none">*请输入正确的企业名称！</span></td>
            </tr>
            <tr>
                <td align="right">企业编码：</td>
                <td>
                    <input type="text" id="ccode" style="width:200px" name="ccode"
                           class="easyui-validatebox easyui-textbox"/>
                </td>
            </tr>
            <tr>
                <td><a href="javascript:uploadidalogonline(1)">上传营业执照图片</a></td>
                <td>
                    <img id="pic" name="license" style="width: 100px;height: 100px">
                    <input id="elicense" width="50px" name="license" hidden>
                </td>
            </tr>
            <tr>
                <td align="right">法人代表：</td>
                <td>
                    <input type="text" id="elegal" style="width:200px" name="legal"
                           class="easyui-validatebox easyui-textbox"
                           data-options="required:true,validType:'length[2,10]'"/>
                </td>
                <td id="velegal"><span style="color: red;display: none">*请输入正确的企业名称！</span></td>
            </tr>
            <tr>
                <td align="right">企业地址：</td>
                <td>
                    <input type="text" id="eaddress" style="width:200px" name="address"
                           class="easyui-validatebox easyui-textbox"
                           data-options="required:true,validType:'length[10,30]'"/>
                </td>
            </tr>
            <tr>
                <td align="right">联系电话：</td>
                <td>
                    <input type="text" id="etel" style="width:200px" name="tel"
                           class="easyui-validatebox easyui-textbox"
                           data-options="required:true,validType:'length[11,11]'"/>
                </td>
            </tr>
            <tr>
                <td align="right">备注信息：</td>
                <td>
                    <textarea rows="10" cols="50" id="remark" prompt="企业描述信息" name="remark" class="easyui-textarea"
                              style="width:330px"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

<div id="dlg-buttons">
    <a href="javascript:saveEnterprise()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeEnterpriseDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
<div id="uploadonlineinfo" class="easyui-dialog" style="width:380px;padding:10px 40px" closed="true">
    <form id="fam" method="post" enctype="multipart/form-data">
        <input type="file" id="file" name="file">
        <input type="hidden" id="flag" name="flag">
    </form>

    <div data-options="region:'south',border:false" style="text-align:center;padding:5px 0 0;">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)"
           onclick="uploadonline();" style="width:80px">上传</a>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $("#dg").datagrid({
            onLoadSuccess: function (data) {
                $('.enterprise-easyui-linkbutton-edit').linkbutton({text: '编辑', plain: true, iconCls: 'fa fa-pencil'});
                $('.enterprise-easyui-linkbutton-del').linkbutton({text: '删除', plain: true, iconCls: 'fa fa-trash-o'});
                $('.enterprise-easyui-linkbutton-ischeck').linkbutton({text: '未认证', plain: true, iconCls: 'fa fa-pencil'});
                $('.enterprise-easyui-linkbutton-nocheck').linkbutton({text: '已认证', plain: true, iconCls: 'fa fa-trash-o'});
                // $('.enterprise-easyui-linkbutton-nocheck').attr("disabled",true);
                $('#dg').datagrid('fixRowHeight');
            }
        });
    })

    function uploadidalogonline(flag) {
        $('#uploadonlineinfo').dialog('open').dialog('setTitle', '文件上传');
        $("#flag").val(flag);
    }

    function uploadonline() {
        var file = $("#file")[0].files[0];
        var flag = $("#flag").val();
        var url;
        if (file.name == "") {
            alert("请先选择文件");
            return false;
        }
        if (flag == 1) {
            url = "/image/upload";
            if (!file.name.match(/.jpg|.jpeg|.gif|.png|.bmp/i)) {
                alert("文件格式错误");
                return false;
            }
        } else {
            url = "/sys/enterprise/excelImport";
            if (!file.name.match(/.xls|.xlsx/i)) {
                alert("文件格式错误");
                return false;
            }
        }
        var form = new FormData();
        form.append("file", file);
        var picget = "/image/show?fileName=";
        $.ajax({
            url: url,
            type: "post",
            data: form,
            processData: false,
            contentType: false,
            success: function (data) {
                $('#uploadonlineinfo').window('close');
                if (flag == 1) {
                    $("#pic").attr("src", picget + data.message);
                    $("#elicense").val(data.message);
                }
                console.log(data);
            },
            error: function (e) {
                console.log(e);
            }
        });

    }
</script>
</body>
</html>